<?php
	/** sous fonctionnalité de modification des infos d'un membre
	 * @author Sebastian Mosser
	 */

class sfMemberInfosEdit extends aSubFeature {


	public function __construct() {
		  $this->_acl = new AccessControlList(); 
      $this->_acl->setGranted(3);
      $this->_acl->setGranted(7);
		}


	protected function run() {
		$action = PropRetriever::getProp($_POST,"action");
		if ($action == "doEdit")
			return $this->updateAMember();
		else
			return $this->displayForm();
	}
	
	
	   private function updateAMember() {
//   	var_dump($_POST);
   	// Récupération des champs de formulaire
   	 $id = PropRetriever::getProp($_POST,"memberId");
   	 $lastName = PropRetriever::getProp($_POST,"lastName");
   	 $firstName = PropRetriever::getProp($_POST,"firstName");
   	 $birthDate = PropRetriever::getProp($_POST,"birthDate");
   	 $gender = PropRetriever::getProp($_POST,"gender");
   	 $birthCity = PropRetriever::getProp($_POST,"birthCity");
   	 $adress = PropRetriever::getProp($_POST,"adress");
   	 $livingCity = PropRetriever::getProp($_POST,"livingCity");
   	 $phone = PropRetriever::getProp($_POST,"phone");
   	 $cellPhone = PropRetriever::getProp($_POST,"cellPhone");
   	 $licence = PropRetriever::getProp($_POST,"licence");
   	 $entryDate = PropRetriever::getProp($_POST,"entryDate");
   	 $web = PropRetriever::getProp($_POST,"web");
   	
   	 // Traitement des erreurs 
   	 $errors = array();
   	 if ($lastName == "")
   	 	 $errors["lastName"] = "Le nom de famille ne peut pas être vide !";
   	 
   	 if ($firstName == "")
   	 	 $errors["firstName"] = "Le prénom ne peut pas être vide !";
   	 
   	 if ($birthDate == "")
   	 	$errors["birthDate"] = "La date de naissance ne peut pas être vide !";
   	 else if (! Date::isValidDate($birthDate))
   	 		$errors["birthDate"] = "La date de naissance est invalide !";
   	 	
   	 	if ($phone != "" && (strlen($phone) != 10 || ! is_numeric($phone)))
   	 		$errors["phone"] = "Le numéro de téléphone fixe est incorrect !";
   	 	else if ($phone != "" && $phone[0] != 0)
   	 		$errors["phone"] = "Le numéro de téléphone doit commencer par '0' !";
   	 		
   	 		if ($cellPhone != "" && 
   	 				(strlen($cellPhone) != 10 || ! is_numeric($cellPhone)))
   	 		$errors["cellPhone"] = "Le numéro de téléphone mobile est incorrect !";
   	 	else if ($cellPhone != "" && ($cellPhone[0] != 0 || $cellPhone[1] != 6))
   	 		$errors["phone"] = "Le numéro de téléphone mobile doit commencer par '06' !";
   	 	
   	 	if ($entryDate == "")
   	 	  $errors["entryDate"] = "La date d'entrée ne peut pas être vide !";
   	  else if (! Date::isValidDate($entryDate))
   	 		$errors["entryDate"] = "La date d'entrée est invalide !";
   	 	
   	 	
   	 // Il y a des erreurs : 
   	 if (count($errors) != 0)
   	 	 return $this->displayForm($errors);
   	 else {
   	 
   	 	 // Instanciation du membre
   	 	 $aMember = Member::read($id);
   	 	 $aMember->setLicenceNo($licence);
   	 	 $aMember->setEntryDate(Date::fromSql($entryDate));
   	 	 $aMember->setLastName(ucwords(strtolower(stripslashes($lastName))));
   	 	 $aMember->setFirstName(ucwords(strtolower(stripslashes($firstName))));
   	 	 $aMember->setGender($gender);
   	 	 $aMember->setAddress(stripslashes($adress));
   	 	 $aMember->setLivingCity($livingCity);
   	 	 $aMember->setBirthDate(Date::fromSql($birthDate));
   	 	 $aMember->setBirthCity($birthCity);
   	 	 $aMember->setPhone($phone == "" ? "" : "+33".substr($phone,1));
   	 	 $aMember->setCellPhone($cellPhone == ""? "": "+33".substr($cellPhone,1));
   	 	 if ($web == "True")
   	 	 	 $aMember->setExportable(True);
   	 	 else
   	 	 	 $aMember->setExportable(False);
   	 	 
   	 	 
   	 	 $aMember->update();
   	 	 //var_dump($_POST);
   	 	 //var_dump($aMember);
   	 	 header("Location: sub_editMember.php?id=$id");
   	 }
   }
	
	
	private function displayForm($errors = array()) {
		$memberId = PropRetriever::getProp($_POST,"memberId");

		$aMember = Member::read($memberId);		
		
		if (PropRetriever::getProp($_POST,"lastName") == "")
			$lastName = $aMember->getLastName();
		else
			$lastName = PropRetriever::getProp($_POST,"lastName");
		
		if (PropRetriever::getProp($_POST,"firstName") == "")
			$firstName = $aMember->getFirstName();
		else
			$firstName = PropRetriever::getProp($_POST,"firstName");
			
		if (PropRetriever::getProp($_POST,"gender") == "")
			$gender = $aMember->getGender();
		else
			$gender = PropRetriever::getProp($_POST,"gender");
			
		if ($gender != Member::$FEMALE) {
   	 	$sex = 
   	 		"<option value=\"".Member::$MALE."\" selected > Masculin </option>";
   	 	$sex .= 
   	 		"<option value=\"".Member::$FEMALE."\" > Féminin </option>";
   	 }
   	 else {
   	 	$sex = 
   	 		"<option value=\"".Member::$MALE."\" > Masculin </option>";
   	 	$sex .= 
   	 		"<option value=\"".Member::$FEMALE."\" selected > Féminin </option>";
   	 }	
   	 
   	 
   	 
   	if (PropRetriever::getProp($_POST,"birthDate") == "")
			$birthDate = $aMember->getBirthDate()->sqlFormat();
		else
			$birthDate = PropRetriever::getProp($_POST,"birthDate");
		
		if (PropRetriever::getProp($_POST,"birthCity") == "")
			$birthCity = $aMember->getBirthCity();
		else
			$birthCity = PropRetriever::getProp($_POST,"birthCity");
		
		$birthZip = "";
		if (PropRetriever::getProp($_POST,"birthZip") == "") {
			try{
				$aCity = City::read($birthCity);
				$birthZip = $aCity->getZip();
			}
			catch(Exception $e) {
				$birthZip = "";
			}
		}
		else {
			$birthZip = PropRetriever::getProp($_POST,"birthZip");
		}
		
		$birthZipInput = "";
   	if ($birthZip == "FOREIGN") {
   	 	$birthZipInput = '<input type="hidden" name="birthZip" id="birthZip" value="FOREIGN" type="hidden" />';
   	 	$birthZipInput .= '<input name="na" disabled="yes" value="Etranger" class="edit" />';
   	}
   	else {
   		$birthZipInput = '<input type="text" name="birthZip" id="birthZip" 
							class="edit" value="'.$birthZip.'" onblur="updateBirthCity();"/>';
   	}
		
		$birthCityList = "";
   	 if ($birthZip == "") {
	   	 $birthCityList = '<span id="birthCityList">';
			 $birthCityList .= '<select name="birthCity" class="edit">';
			 $birthCityList .= '<option value="-1" > Inconnue </option>';
			 $birthCityList .= '</select> </span>'; 
			}
			else {
				$optionsBirthCity = City::getAsList($birthZip,$birthCity);
				$birthCityList = '<span id="birthCityList">';
				$birthCityList .= '<select name="birthCity" class="edit">';
				$birthCityList .= $optionsBirthCity;
				$birthCityList .= '</select> </span>'; 
			}
		
		if (PropRetriever::getProp($_POST,"adress") == "")
			$adress = $aMember->getAddress();
		else
			$adress = PropRetriever::getProp($_POST,"adress");
		
		
		
		if (PropRetriever::getProp($_POST,"livingCity") == "")
			$livingCity = $aMember->getLivingCity();
		else
			$livingCity = PropRetriever::getProp($_POST,"livingCity");
		
		$livingZip = "";
		if (PropRetriever::getProp($_POST,"livingZip") == "") {
			try{
				$aCity = City::read($livingCity);
				$livingZip = $aCity->getZip();
			}
			catch(Exception $e) {
				$livingZip = "";
			}
		}
		else {
			$livingZip = PropRetriever::getProp($_POST,"livingZip");
		}
		
		$livingCityList = "";
   	 if ($livingZip == "") {
	   	 $livingCityList = '<span id="livingCityList">';
			 $livingCityList .= '<select name="livingCity" class="edit">';
			 $livingCityList .= '<option value="-1" > Inconnue </option>';
			 $livingCityList .= '</select> </span>';
		}
		else {
			$optionsLivingCity = City::getAsList($livingZip,$livingCity);
			$livingCityList = '<span id="livingCityList">';
			$livingCityList .= '<select name="livingCity" class="edit">';
			$livingCityList .= $optionsLivingCity;
			$livingCityList .= "</select>	</span>"; 	
		}
		
		if (PropRetriever::getProp($_POST,"phone") == "") {
			$phone = "0" . substr($aMember->getPhone(),3);
			$phone = ($phone == "0" ? "" : $phone);
		}
		else
			$phone = PropRetriever::getProp($_POST,"phone");
		
		if (PropRetriever::getProp($_POST,"cellPhone") == "") {
			$cellPhone = "0" . substr($aMember->getCellPhone(),3);
			$cellPhone = ($cellPhone == "0" ? "" : $cellPhone);
		}
		else
			$cellPhone = PropRetriever::getProp($_POST,"cellPhone");
		
		if (PropRetriever::getProp($_POST,"licence") == "")
			$licence = $aMember->getLicenceNo(); 
		else
			$licence = PropRetriever::getProp($_POST,"licence");
			
		if (PropRetriever::getProp($_POST,"entryDate") == "")
			$entryDate = $aMember->getEntryDate()->sqlFormat(); 
		else
			$entryDate = PropRetriever::getProp($_POST,"entryDate");
		
		if (PropRetriever::getProp($_POST,"web") == "")
			$web = ($aMember->getExportable() ? "True" : "False");
		else
			$web = PropRetriever::getProp($_POST,"web");
 		
 		$webOptions = "";
 		if ($web == "False") {
 			$webOptions .= '<option value ="True"> Visible </option>';
 			$webOptions .= '<option value ="False" selected> Invisible </option>';
 		}
 		else {
 			$webOptions .= '<option value ="True" selected> Visible </option>';
 			$webOptions .= '<option value ="False"> Invisible </option>';
 		}
		
		
		
		// Traitement des erreurs :
 		$style = array();
 		$style["lastName"] = ""; $style["firstName"] = "";
 		$style["birthDate"] = ""; $style["phone"] = "";
 		$style["cellPhone"] = ""; $style["entryDate"] = "";
 		
 		$fails = "";
 		foreach($errors as $k => $v) {
 			$style[$k] = " class=\"error\" ";
 			$fails .= "<li> <span class=\"error\"> $v </span> </li>";
 		}
 		if ($fails != "") {
 			$fails = "<h2> Des erreurs sont survenues ! </h2> <p> <ul> " . $fails;
 			$fails .= "</ul> </p>";
 		}
		
		$returnButton = sfMemberEdit::getReturnButton($memberId);
		$page = <<<EOS
<h1> Modification des informations d'un adhérent </h1>
<h2> Explications </h2>
<p> Effectuez les modifications voulues puis cliquez sur <span class="error">[ Appliquer ]</span> pour charger vos modifications dans le système. </p>

<div align="center">
${returnButton}
</div>
<br />
${fails}
<div align="center">

<form method="post" accept-charset="utf-8" name="addMember"
			action="?add" >

<input type="hidden" name="action" value="doEdit" />
<input type="hidden" name="memberId" value="${memberId}" />
<table> 

<tr> 
<td align="center">

	<fieldset>
		<legend> État Civil / Naissance </legend>
		<br />
		
		<label> 
			<span ${style["lastName"]}>Nom : </span>
			<input type="text" name="lastName" class="edit" value="${lastName}" />
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span ${style["firstName"]} >Prénom : </span>
			<input type="text" name="firstName" class="edit" value="${firstName}" />
			&nbsp; &nbsp;
		</label>
		<br />
		<label> 
			<span>Sexe : </span>
			<select name="gender" class="edit" />
			${sex}
			</select>
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span ${style["birthDate"]}>Né le :</span>
			<!-- <input type="text" name="birthDate" class="edit" value="${birthDate}" 
							onclick="javascript:showCal('BirthDateCal')" /> -->
			 <input type="text" name="birthDate" id="birthDate" class="edit" value="${birthDate}" 
							onclick="javascript:displayBalloonCal('birthDate')" />
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span> <a href="#" onclick="foreignBirthCity();">Code Postal</a> : </span>
			<span id="birthZipCode">
			${birthZipInput}
			</span>
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span> <a href="#" onclick="updateBirthCity();" >Ville :</a> </span>
			<span id="cityList">
			${birthCityList}
			</span>
			&nbsp; &nbsp;
		</label>
		<br />  <br />
	</fieldset>
	
</td>

<td> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>

<td align="center">
	<fieldset>
		<legend> Adresse / Contacts </legend>
		<br />
		
		<label> 
		<div align="left"> &nbsp; &nbsp; <strong> Adresse : </strong> </div>
		<textarea name="adress" class="edit">${adress} </textarea> 
		</label>
		<br /> <br />
		
		<label> 
			<span> Code Postal : </span>
			<input type="text" name="livingZip" id="livingZip" 
							class="edit" value="${livingZip}" onblur="updateLivingCity();"/>
			</span>
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span> <a href="#" onclick="updateLivingCity();" >Ville :</a> </span>
			${livingCityList}
			&nbsp; &nbsp;
		</label>
		<br />  

		<label> 
			<span ${style["phone"]} >Tel. Fixe : </span>
			<input type="text" name="phone" class="edit" value="${phone}" />
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span ${style["cellPhone"]}>Tel. Mobile : </span>
			<input type="text" name="cellPhone" class="edit" value="${cellPhone}" />
			&nbsp; &nbsp;
		</label>
		<br />

	</fieldset>

</td>

<td> &nbsp;&nbsp;&nbsp;&nbsp; </td>


<td align="center">
	<fieldset>
		<legend> Infos Club / Fédération </legend>
		<br />
		
		<label> 
			<span>Licence : </span>
			<input type="text" name="licence" class="edit" value="${licence}" />
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span ${style["entryDate"]} >Date d'entrée:</span>
			<input type="text" name="entryDate" class="edit" value="${entryDate}" 
							onclick="javascript:showCal('EntryDateCal')" />
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span>Site Web : </span>
			<select name="web" class="edit" />
			${webOptions}
			</select>
			&nbsp; &nbsp;
		</label>
		
		<br />  <br />
	</fieldset>
</td>

</tr>	

<tr>
 <td> <br /> </td>
 <td> &nbsp;&nbsp;&nbsp;&nbsp; </td>
 <td> <br/> </td>
</tr>

<tr>
  <td> </td> <td> </td>
 <td align="center"> <input type="submit" value="Appliquer" class="edit" />    </td>
 <td> &nbsp;&nbsp;&nbsp;&nbsp; </td>
 <td align="center" > </td>
</tr>
</table>

</form>
</div>

EOS;
		return $page;	
	
	}
	
	
}

?>